<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="编号" prop="num">
        <el-input
            v-model="queryParams.num"
            clearable
            placeholder="请输入编号"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书名" prop="name">
        <el-input
            v-model="queryParams.name"
            clearable
            placeholder="请输入书名"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
            v-model="queryParams.author"
            clearable
            placeholder="请输入作者"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="publisher">
        <el-input
            v-model="queryParams.publisher"
            clearable
            placeholder="请输入出版社"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版时间" prop="publicationDate">
        <el-date-picker v-model="queryParams.publicationDate"
                        clearable
                        placeholder="请选择出版时间"
                        type="date"
                        value-format="YYYY-MM-DD">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" clearable placeholder="请选择类型">
          <el-option
              v-for="dict in bs_common_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" clearable placeholder="请选择状态">
          <el-option
              v-for="dict in bs_common_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="BookBorrowList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column align="center" label="图片" prop="pic" width="100">
        <template #default="scope">
          <image-preview :height="50" :src="scope.row.pic" :width="50"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="编号" prop="num"/>
      <el-table-column align="center" label="书名" prop="name"/>
      <el-table-column align="center" label="作者" prop="author"/>
      <el-table-column align="center" label="出版社" prop="publisher"/>
      <el-table-column align="center" label="出版时间" prop="publicationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.publicationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="位置" prop="location"/>
      <el-table-column align="center" label="类型" prop="type">
        <template #default="scope">
          <dict-tag :options="bs_common_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="state">
        <template #default="scope">
          <dict-tag :options="bs_common_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="描述" prop="description" show-overflow-tooltip/>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template #default="scope">
          <el-button v-hasPermi="['borrow:bookBorrow:edit']" icon="View" link type="primary"
                     @click="handleUpdate(scope.row)"></el-button>
          <el-button v-hasPermi="['borrow:bookBorrow:remove']" icon="Reading" link type="primary"
                     @click="handleBorrow(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 查看图书对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="500px">
      <el-form ref="BookBorrowRef" :model="form" :rules="rules" disabled label-width="80px">
        <el-form-item label="图片" prop="pic">
          <image-upload v-model="form.pic"/>
        </el-form-item>
        <el-form-item label="编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入编号"/>
        </el-form-item>
        <el-form-item label="书名" prop="name">
          <el-input v-model="form.name" placeholder="请输入书名"/>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者"/>
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社"/>
        </el-form-item>
        <el-form-item label="出版时间" prop="publicationDate">
          <el-date-picker v-model="form.publicationDate"
                          clearable
                          placeholder="请选择出版时间"
                          type="date"
                          value-format="YYYY-MM-DD">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
                v-for="dict in bs_common_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
                v-for="dict in bs_common_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="BookBorrow" setup>
import {getBookBorrow, insertBookBorrow, listBookBorrow} from "@/api/borrow/BookBorrow";

const {proxy} = getCurrentInstance();
const {bs_common_type, bs_common_status} = proxy.useDict('bs_common_type', 'bs_common_status');

const BookBorrowList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    num: null,
    name: null,
    author: null,
    publisher: null,
    publicationDate: null,
    type: null,
    state: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

/** 查询借阅处理列表 */
function getList() {
  loading.value = true;
  listBookBorrow(queryParams.value).then(response => {
    BookBorrowList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    pic: null,
    num: null,
    name: null,
    author: null,
    publisher: null,
    publicationDate: null,
    location: null,
    type: null,
    state: null,
    description: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateTime: null,
    updateBy: null
  };
  proxy.resetForm("BookBorrowRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 查看操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBookBorrow(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "书籍详细信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["BookBorrowRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        open.value = false;
        getList();
      }
    }
  })
}

/** 借阅按钮操作 */
function handleBorrow(row) {
  const num = row.num;
  const bookName = row.name || "未知";
  const author = row.author || "未知";
  proxy.$modal.confirm('是否确认借阅《"' + bookName + '"》 | 作者：' + author + '？').then(function () {
    return insertBookBorrow(num);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("借阅成功");
  }).catch(() => {
  });
}

getList();
</script>
